This RMarkdown file contains an examples of interactive plots in R.
devtools::install_github('hadley/ggplot2')
pacman::p_load(plotly, dplyr)
data(txhousing)
tx = txhousing %>%
select(city, month, median, year) %>%
filter(city %in% c('Galveston', 'Bay Area', 'Port Arthur', 'Austin')) %>%
group_by(city, year) %>%
summarise(avg = mean(median)) %>%
na.omit() %>%
data.frame()
nrow(tx)
## [1] 60
head(tx)
## city year avg
## 1 Austin 2000 143925.0
## 2 Austin 2001 149991.7
## 3 Austin 2002 154191.7
## 4 Austin 2003 154058.3
## 5 Austin 2004 153250.0
## 6 Austin 2005 160891.7
texas_plot = ggplot(tx, aes(x = year, y = avg, color = city), factor = city) +
geom_path() +
labs(y = 'Average price ($USD)',
title = 'Texas housing prices 2000-2015, by month') +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
scale_x_continuous(breaks = pretty(tx$year, n = 15))
print(texas_plot)
gg = ggplotly(texas_plot, session="knitr")
gg
texas_plotly = plot_ly(data = tx, x = ~year, y = ~avg) %>%
add_lines(color = ~city) %>%
rangeslider() %>%
layout(xaxis = list(title = ""))
#(texas_plotly, "texas_plotly.png")
Here is a static image of the plot, or click here to access my plotly account for the interactive version.